/*M
 * Module-Name : fmttbl.c
 *
 * Purpose     : standard main-module with prototypes
 *
 * Created     : xxx yyyy/mm/dd
 *
 * Copyright (C) 1995 INTRA-SYS GmbH Hannover, Germany. All rights reserved.
 *
 * $Log: fmttbl.c,v $
 * Revision 1.2  2001/08/07 14:21:23  ott
 * generelles Neu-Formatieren des Source
 *
 * Revision 1.1.1.1  1998/11/21 18:09:26  ott
 * erster Import
 *
 */

/*--------------------------------------------- Include-Files -----------*/

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <memory.h>

#define N_LINES		21
#define N_LEFT		( N_LINES - 1)
#define N_FULL		( (N_LEFT * 2) + 1 )
#define L_LEFT		44
#define L_LR		( L_LEFT * 2 )
#define L_INBUF		127

/*--------------------------------------------- Function Definitions ----*/

static void	AddLine (char *);
static void	PrintTable (void);

/*--------------------------------------------- Extern References -------*/

/*--------------------------------------------- Static References -------*/

static char	*RcsId = "$Id: fmttbl.c,v 1.2 2001/08/07 14:21:23 ott Exp $";

static int	nitem;
static int	TabPerPage;

static char	Inbuf [L_INBUF + 1];
static char	OutTab [N_LINES] [L_LR];

/*--------------------------------------------- Global References -------*/

/*--------------------------------------------- Functions ---------------*/

/*F
 * Function-Name : void main (argc, argv)
 *
 * Parameters    : int argc - argument count
 *                 char *argv [] - argument strings
 *
 * Return-Values : none
 *
 * Purpose       :
 */

int
main (int argc, char **argv)
{
  char *p;

  p = RcsId;

  if (argc != 1) {
      (void) fprintf (stderr, "usage: %s <in-tbl >out-tbl\n", argv [0]);
      return (1);
  }

  while (fgets (Inbuf, L_INBUF, stdin) != (char *) 0) {
      (void) fputs (Inbuf, stdout);

      if (strchr (Inbuf, '\f') != (char *) 0) {
          break;
      }
  }

  while (fgets (Inbuf, L_INBUF, stdin) != (char *) 0) {
      if ((p = strpbrk (Inbuf, "\n\f")) != (char *) 0) {
          *p = 0x00;
      }

      if (strlen (Inbuf) > 0) {
          (void) AddLine (Inbuf);
      }
  }

  if (nitem > 0) {
      (void) PrintTable ();
  }

  return (0);
}

/*-----------------------------------------------------------------------*/

/*F
 * Function-Name :
 *
 * Parameters    :
 *
 * Return-Values :
 *
 * Purpose       :
 */

static void
AddLine (char *buf)
{
  if (nitem == 0) {
      int i;

      for (i = 0; i < N_LINES; ++i) {
           (void) memset (OutTab [i], 0x00, L_LR);
           (void) memset (OutTab [i], 0x20, L_LEFT + 1);
      }
  }

  if (nitem <= N_LEFT) {
      (void) memcpy (OutTab [nitem], buf, strlen (buf));
  }
  else {
      (void) strcpy (OutTab [nitem - N_LINES] + L_LEFT, buf);
  }

  ++nitem;

  if (nitem > N_FULL) {
      (void) PrintTable ();
      nitem = 0;
  }
}

/*-----------------------------------------------------------------------*/

/*F
 * Function-Name :
 *
 * Parameters    :
 *
 * Return-Values :
 *
 * Purpose       :
 */

static void
PrintTable (void)
{
  int i;

  for (i = 0; i < N_LINES; ++i) {
      (void) printf ("%s\n", OutTab [i]);
  }

  (void) printf ("\n");

  ++TabPerPage;

  if (TabPerPage > 2) {
      TabPerPage = 0;
      (void) printf ("\f");
  }
}

/*-----------------------------------------------------------------------*/

/* fmttbl.c */
